﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MidiMix
{
    public partial class ChannelStrip : UserControl
    {
        private ChannelStripSettings channelStripSettings = new ChannelStripSettings();
        public ChannelStripSettings ChannelStripSettings {
            get{ return channelStripSettings;}
        }
        public bool ShowOnPiano
        {
            get { return channelStripSettings.ShowOnPiano;
            }
        }
        public bool Mute
        {
            get
            {
                return channelStripSettings.Mute;
            }
        }
        public bool Solo
        {
            get
            {
                return channelStripSettings.Solo;
            }
        }
        public Color TrackColor
        {
            get { return channelStripSettings.TrackColor; }
            set { 
                panelColor.BackColor = value;
                channelStripSettings.TrackColor = value;
            }
        }
        public string TrackNum
        {
            get { return channelStripSettings.TrackNum; }
            set { 
                labelTrackNumber.Text = value; 
                channelStripSettings.TrackNum = value;
            }
        }
        public string TrackName
        {
            get { return channelStripSettings.TrackName; }
            set { 
                labelTrackName.Text = value; 
                channelStripSettings.TrackName=value;
            }
        }
        public float ValuePercent
        {
            get { return channelStripSettings.ValuePercent; }
        }

        public ChannelStrip()
        {
            InitializeComponent();
            channelStripSettings.Mute = checkBoxMute.Checked;
            channelStripSettings.Solo = checkBoxSolo.Checked;
            channelStripSettings.ShowOnPiano = checkPiano.Checked;
            channelStripSettings.TrackColor = panelColor.BackColor;
            channelStripSettings.TrackName = labelTrackName.Text;
            channelStripSettings.TrackNum = labelTrackNumber.Text;
            channelStripSettings.ValuePercent = (float)trackBar1.Value / (float)trackBar1.Maximum; 
        }
        public void UpdateControls()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(Refresh));
                return;
            }
            checkBoxMute.Checked = channelStripSettings.Mute;
            checkBoxSolo.Checked = channelStripSettings.Solo;
            checkPiano.Checked = channelStripSettings.ShowOnPiano;
            trackBar1.Value = (int)(channelStripSettings.ValuePercent*trackBar1.Maximum);
            panelColor.BackColor = channelStripSettings.TrackColor;
            labelTrackName.Text = channelStripSettings.TrackName; 
            labelTrackNumber.Text = channelStripSettings.TrackNum;
            
        }
        private void ChannelStrip_Load(object sender, EventArgs e)
        {
            this.channelStripSettings.ValuePercent = (float)trackBar1.Value / (float)trackBar1.Maximum;
        }
        private void checkBoxMute_CheckedChanged(object sender, EventArgs e)
        {
            channelStripSettings.Mute = checkBoxMute.Checked;

            if (checkBoxMute.Checked) checkBoxSolo.Checked = false;
            checkBoxMute.FlatAppearance.MouseOverBackColor = checkBoxMute.FlatAppearance.MouseDownBackColor = 
                (checkBoxMute.Checked) ? Color.DarkGoldenrod: Color.White;

        }

        private void checkBoxSolo_CheckedChanged(object sender, EventArgs e)
        {
            channelStripSettings.Solo = checkBoxSolo.Checked;
            if (checkBoxSolo.Checked) checkBoxMute.Checked = false;
            checkBoxSolo.FlatAppearance.MouseOverBackColor = checkBoxSolo.FlatAppearance.MouseDownBackColor =
                (checkBoxSolo.Checked) ? Color.DarkGoldenrod : Color.White;
        }

        private void checkPiano_CheckedChanged(object sender, EventArgs e)
        {
            channelStripSettings.ShowOnPiano = checkPiano.Checked;
        }


        private void panelColor_Click(object sender, EventArgs e)
        {
            DialogResult result = colorDialog.ShowDialog();

            if (DialogResult.OK ==  result) {
                panelColor.BackColor = colorDialog.Color;
                channelStripSettings.TrackColor = colorDialog.Color;
            }
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            this.channelStripSettings.ValuePercent = (float)trackBar1.Value / (float) trackBar1.Maximum;
        }

    }
}
